﻿using System;
using System.Linq;
using JetBrains.ReSharper.Daemon;
using JetBrains.ReSharper.Daemon.Stages;
using JetBrains.ReSharper.Daemon.Xml.Highlightings;
using JetBrains.ReSharper.Psi.Tree;
using JetBrains.ReSharper.Psi.Xml;
using JetBrains.ReSharper.Psi.Xml.Tree;
using SPCAFContrib.Entities.Consts;
using SPCAFContrib.ReSharper.Common;
using SPCAFContrib.ReSharper.Common.Attributes;
using SPCAFContrib.ReSharper.Common.Extensions;
using SPCAFContrib.ReSharper.Common.XmlAnalysis;
using SPCAFContrib.ReSharper.Consts;
using SPCAFContrib.ReSharper.Inspection.Xml.SPCAF;

[assembly: RegisterConfigurableSeverity(SPC017702Highlighting.CheckId,
  null,
  Consts.SPCAF_CORRECTNESS_GROUP,
  SPC017702Highlighting.CheckId + ": " + SPC017702Highlighting.Message,
  "A WebTemplate must derive from an existing Template, e.g from a template in TEMPLATE\\1033\\XML\\WEBTEMP.xml.",
  Severity.ERROR,
  false, Internal = false)]

namespace SPCAFContrib.ReSharper.Inspection.Xml.SPCAF
{
    [Applicability(
        IDEProjectType.SP2010FarmSolution |
        IDEProjectType.SPSandbox |
        IDEProjectType.SP2013FarmSolution)]
    public class DefineValidParentWebTemplate : XmlTagProblemAnalyzer
    {
        public override void Run(IXmlTag element, IHighlightingConsumer consumer)
        {
            if (element.GetProject().IsApplicableFor(this))
            {
                if (IsInvalid(element))
                {
                    SPC017702Highlighting errorHighlighting = new SPC017702Highlighting(element);
                    consumer.ConsumeHighlighting(element.Header.GetDocumentRange(), errorHighlighting);
                }
            }
        }

        public static bool IsInvalid(IXmlTag element)
        {
            bool result = false;

            if (element.Header.ContainerName == "WebTemplate" && 
                element.AttributeExists("BaseTemplateID") && 
                element.AttributeExists("BaseTemplateName") && 
                element.AttributeExists("BaseConfigurationID"))
            {
                int BaseTemplateID = Int32.Parse(element.GetAttribute("BaseTemplateID").UnquotedValue);
                string BaseTemplateName = element.GetAttribute("BaseTemplateName").UnquotedValue.Trim();
                int BaseConfigurationID = Int32.Parse(element.GetAttribute("BaseConfigurationID").UnquotedValue);
                result =
                    !TypeInfo.WebTemplates.Any(
                        wt =>
                            wt.Id == BaseTemplateID && wt.Title == BaseTemplateName &&
                            wt.Configurations.Any(c => c.Id == BaseConfigurationID));
            }

            return result;
        }
    }

    [ConfigurableSeverityHighlighting(CheckId, XmlLanguage.Name, OverlapResolve = OverlapResolveKind.NONE, ShowToolTipInStatusBar = true)]
    public class SPC017702Highlighting : XmlErrorHighlighting
    {
        public const string CheckId = "SPC017702";
        public const string Message = "Derive WebTemplate from an existing Template";

        public IXmlTag Element { get; private set; }

        public SPC017702Highlighting(IXmlTag element) :
            base(String.Format("{0}: {1}", CheckId, Message))
        {
            Element = element;
        }
    }
}
